#!/bin/bash
cd `dirname $0`

img_mvn="maven:3.3.3-jdk-8"        # docker image of maven
m2_cache=~/.m2                     # the local maven repository dir
proj_home=$PWD                     # the project root dir
img_name="deepexi/adapter-service" # output image tag
c_name="adapter-service"           # container name

git pull  # should use git clone https://name:pwd@xxx.git

build_cmd="mvn clean package -U -Dmaven.test.skip=true"
if which mvn ; then
    echo "use local maven"
    eval $build_cmd
else
    echo "use docker maven"
    docker run --rm \
        -v $m2_cache:/root/.m2 \
        -v $proj_home:/usr/src/mymaven \
        -w /usr/src/mymaven $img_mvn $build_cmd
fi

sudo mv $proj_home/adapter-service/target/adapter-service-*.jar $proj_home/demo.jar # 兼容所有sh脚本
docker build -t $img_name .


# 删除容器
docker rm -f $c_name &> /dev/null

version=`date "+%Y%m%d%H"`

spring_datasource_url=jdbc:mysql://111.230.140.238:3306/car-deepexi-moby-adapter?autoReconnect=true\&useUnicode=true\&characterEncoding=utf-8\&useSSL=false

# 启动镜像
#     --add-host=bdlpidemo.xbotech.com:192.168.101.130 \


docker run -d --restart=on-failure:5 --privileged=true \
    -p 8001:8001 \
    -w /home \
    -v $PWD/logs:/home/logs \
    --name $c_name $img_name \
    java \
        -Djava.security.egd=file:/dev/./urandom \
        -Duser.timezone=Asia/Shanghai \
        -XX:+PrintGCDateStamps \
        -XX:+PrintGCTimeStamps \
        -XX:+PrintGCDetails \
        -XX:+HeapDumpOnOutOfMemoryError \
        -Xloggc:logs/gc_$version.log \
        -jar /home/demo.jar \
          --spring.profiles.active=prod \
          --spring.datasource.url=$spring_datasource_url \
          --spring.datasource.username=root \
          --spring.datasource.password=my-secret-ab \
          --mq.namesrvAddr=118.31.184.251:9876 \
          --sap.sdk.host=http://bdlpidemo.xbotech.com:50000 \
          --moby.sdk.host.car-center=http://193.112.4.11:8001/car-deepexi-car-center \
          --moby.sdk.host.product-center=http://193.112.4.11:8001/car-deepexi-product-center

# 所有环境变量
# https://moby.xbotech.cn/moby-mall/car/
# https://moby.xbotech.cn/moby-mall/product/
# sap.sdk.profile=prod
# sap.sdk.host=http://bdlpidemo.xbotech.com:50000
# 219.143.223.59
# sap.sdk.name=ifs2pi
# sap.sdk.password=BdlPi#123
#
# moby.sdk.profile=prod
# moby.sdk.host.car-center=http://193.112.4.11:8001/car-deepexi-car-center
# moby.sdk.host.product-center=http://193.112.4.11:8001/car-deepexi-car-center
#
# mq.namesrvAddr=118.31.184.251:9876

docker logs -f $c_name
